<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>教师管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="pageQuery" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" clearable v-model="pageQuery.name" placeholder="教师名称"></el-input>
      </el-form-item>
      <el-form-item label="职工号">
        <el-input size="small" clearable v-model="pageQuery.number" placeholder="职工号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-button type="warning" @click="addTeacher" style="margin-bottom: 20px">添加教师</el-button>
    <el-table size="small" :data="teacherList" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <!-- <el-table-column align="center" type="selection" width="60">
      </el-table-column> -->
      <el-table-column align="center" prop="name" label="教师名称" width="200">
      </el-table-column>
      <el-table-column align="center" prop="number" label="职工号" width="200">
      </el-table-column>
      <el-table-column align="center" prop="password" label="密码" width="200">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="200">
        <template slot-scope="scope">
          <el-button size="mini" @click="editTeacher(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deleteTeacher(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageQuery" :total="total" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="editTitle" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="教师名称" prop="name">
          <el-input size="small" v-model="editForm.name" auto-complete="off" placeholder="请输入教师名称"></el-input>
        </el-form-item>
        <el-form-item label="职工号" prop="number">
          <el-input size="small" v-model="editForm.number" auto-complete="off" placeholder="请输入职工号"></el-input>
        </el-form-item>
        <!-- <el-form-item label="等级" prop="level">
          <el-select v-model="editForm.level" clearable placeholder="请选择教师等级">
            <el-option v-for="(item, index) in levelList" :key="index" :label="item.label" :value="item.id">{{item.label}}</el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm(editOrAdd ? 'editForm' : 'addForm')">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '../../components/Pagination'
export default {
  name:'TeacherDetail',
  data() {
    return {
      editOrAdd: true, // 编辑 / 新增 => true / false
      teacherList: [], //教师列表
      pageQuery: {
        name: '',
        number: '',
        pageNum: 1,
        pageSize: 10
      },
      total: 0,
      loading: false, //是显示加载
      editFormVisible: false, // 编辑框是否弹出
      editTitle: '编辑教师',
      editForm: {
        id: '',
        name: '',
        number: '',
      },
      // rules表单验证
      rules: {
        name: [
          { required: true, message: '请输入教师名称', trigger: 'blur' }
        ],
        number: [{ required: true, message: '请输入职工号码', trigger: 'blur' }],
      },
      levelList: [
      ]
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  created() {
    this.getTeacherList()
  },
  methods: {
    /**
     * 搜索
     */
    search () {
      this.getTeacherList()
    },
    /**
     * 编辑弹框保存教师修改信息
     */
    submitForm (msg) {
      if (msg === 'editForm') {
        this.$api.manager.discTeacher(this.editForm).then(res => {
          if (res.code === 200) {
            this.editFormVisible = false
            this.getTeacherList()
          }
        }).catch(err => this.editFormVisible = false)
      } else {
        delete this.editForm.id
        this.$api.manager.addTeacher(this.editForm).then(res => {
          if (res.code === 200) {
            this.editFormVisible = false
            this.getTeacherList()
          }
        })
      }
    },
    /**
     * 获取教师列表
     */
    getTeacherList() {
      this.loading = true
      this.$api.manager.getTeacherList(this.pageQuery).then(res => {
        if (res.code === 200) {
          this.teacherList = res.data.records
           this.total = res.data.total
          this.loading = false
        }
      }).catch(err => this.loading = false)
    },
    /**
     * 分页插件事件
     */
    callFather(parm) {
      this.pageQuery.pageNum = parm.currentPage
      this.pageQuery.pageSize = parm.pageSize
      this.getTeacherList()
    },
    /**
     * 关闭编辑、增加弹出框
     */
    closeDialog() {
      this.editFormVisible = false
    },
    /**
     * 编辑教师
     */
    editTeacher (teacher) {
      this.editOrAdd = true
      this.editFormVisible = true
      this.editTitle = "编辑教师"
      this.editForm.id = teacher.id
      this.editForm.name = teacher.name
      this.editForm.number = teacher.number
    },
    /**
     * 增加教师
     */
    addTeacher () {
      this.editOrAdd = false
      this.editFormVisible = true
      this.editTitle = "添加教师"
      this.editForm.name = ''
      this.editForm.number = ''
    },
    /**
     * 删除教师
     */
    deleteTeacher (index, teacher) {
      this.$api.manager.deleteTeacher({id:teacher.id}).then(res => {
        if (res.code === 200) {
          this.getTeacherList()
        }
      })
    }
  }
}
</script>

<style>
.teacher-search {
  margin-top: 20px;
}
.teacherRole {
  width: 100%;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

 
 